package com.kinglone.controller;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.kinglone.model.User;
import com.kinglone.model.UserVo;
import com.kinglone.service.UserService;
import com.kinglone.util.ExcelUtil;
import com.kinglone.util.JSONMessageView;
/**
 * 导入excel
 * @author Administrator
 *
 */
@Controller
public class ExcelDemoController {
	
	@Resource  
	private UserService userService;  
	
	/**
	 * 跳转方式一导入excel页面
	 * @param request
	 * @param model
	 * @return
	 */
	 @RequestMapping("/showExcel.jspx")  
     public String showExcel(HttpServletRequest request,Model model){  
        return "readExcel";
	}
	 /**
	  * 方式一跳转页面方式导入excel
	  * @param file
	  * @param request
	  * @return
	  */
	 @RequestMapping("/readExcel.jspx")  
	 public ModelAndView readExcel(@RequestParam(value="excelFile") MultipartFile file,HttpServletRequest request){  
		 ModelAndView mv = new ModelAndView("message");
		 try {
			  String name = file.getOriginalFilename();  
			  long size = file.getSize();  
			  
			  System.out.println("文件名称为："+name);
			  System.out.println("文件大小为："+size);
			  if(name == null && size==0){  
			        mv.addObject("msg", "导入失败!");    
			        return mv;  
			 }			
			  InputStream in = file.getInputStream();
			  //读取Excel数据到List中  
			 List<UserVo> list = ExcelUtil.importDataFromExcel(in,name);
			 System.out.println("===list==:"+list);	
			 
			 //查询所有的username集合
			 List<String> userNameList = userService.getListByUserName();				
			
			 for(int i=0;i<list.size();i++){		
				 User user = new User();
				 user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				 user.setUsername(list.get(i).getUsername());
				 System.out.println("----userVo---:"+list.get(i).getUsername());
				 user.setPassword(list.get(i).getPassword());
				 user.setAge(list.get(i).getAge());
				 System.out.println("----username----:"+user.getUsername());
				 userService.save(user);			 			 
			 }
			 mv.addObject("msg", "导入成功!");    	 			  
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		 return mv;
	 }

	 /**
	  * 跳转方式二ajax导入excel页面
	  * @param request
	  * @param model
	  * @return
	  */
	 @RequestMapping("/showAjaxExcel.jspx")  
     public String showAjaxExcel(HttpServletRequest request,Model model){  
        return "readAjaxExcel";
	} 
	 
	/**
	 * 方式二导入excel,使用ajax方式
	 * @param file
	 * @param request
	 * @return
	 */
	 @RequestMapping("/readAjaxExcel.jspx")
	public JSONMessageView readAjaxExcel(@RequestParam(value="excelFile") MultipartFile file,HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "导入失败!", null);
		try {
			
			String name = file.getOriginalFilename();  
			  long size = file.getSize();  
			  
			  System.out.println("文件名称为："+name);
			  System.out.println("文件大小为："+size);
			  if(name == null && size==0){  
			        json.setCode(-1);
			        json.setMessage("请选择文件");
			        return json;
			 }			
			  InputStream in = file.getInputStream();
			  //读取Excel数据到List中  
			 List<UserVo> list = ExcelUtil.importDataFromExcel(in,name);
			 System.out.println("===list==:"+list);	
			 
			 //查询所有的username集合
			 List<String> userNameList = userService.getListByUserName();				
			
			 for(int i=0;i<list.size();i++){		
				 User user = new User();
				 user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				 user.setUsername(list.get(i).getUsername());
				 System.out.println("----userVo---:"+list.get(i).getUsername());
				 user.setPassword(list.get(i).getPassword());
				 user.setAge(list.get(i).getAge());
				 System.out.println("----username----:"+user.getUsername());
				 userService.save(user);			 			 
			 }
			json.setCode(0);
			json.setMessage("导入成功!");
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("导入失败!");
			return json;
		}
	}
	 
}
